package com.frkj.yuan.fragment.home;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.frkj.yuan.R;
import com.frkj.yuan.activity.HospitalListActivity;
import com.frkj.yuan.activity.JiuZhengPesonalActivity;
import com.frkj.yuan.activity.UserLoginActivity;
import com.frkj.yuan.adapter.BaseRecyclerHolder;
import com.frkj.yuan.adapter.BaseRecyclerRefreshAdapter;
import com.frkj.yuan.api.bean.MapPoiListBean;
import com.frkj.yuan.api.bean.ProvinceCityEntity;
import com.frkj.yuan.api.bean.SelectServiceTimeBean;
import com.frkj.yuan.api.category.ProvinceCityCategory;
import com.frkj.yuan.api.presenter.ProvinceCityPresenter;
import com.frkj.yuan.base.BaseFragment;
import com.frkj.yuan.dialog.LoginOutDialog;
import com.frkj.yuan.dialog.SelectTimeDialog;
import com.frkj.yuan.fragment.home.activity.YuyueKeShiActivity;
import com.frkj.yuan.fragment.home.activity.YuyueOrderActoivity;
import com.frkj.yuan.main.ConstantStaticUrl;
import com.frkj.yuan.utils.CardUtils;
import com.frkj.yuan.utils.HttpNetworkConnectUtils;
import com.frkj.yuan.utils.SPUtils;
import com.frkj.yuan.utils.ToastUtil;
import com.frkj.yuan.view.MyTextView;
import com.frkj.yuan.view.RecyclerViewLayoutManager;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * 信息说明: 首页
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/3/28 16 ：58
 * 项目名称：yuanban
 */
public class HomeNewFragment extends BaseFragment implements ProvinceCityCategory.View {
    @BindView(R.id.linar_top)
    RelativeLayout mLinearLayout;
    @BindView(R.id.card_home)
    CardView cardView;
    @BindView(R.id.address)
    TextView mAddress;
    @BindView(R.id.icon_iamge_home)
    ImageView mIoncImage;
    @BindView(R.id.pzy_nub)
    MyTextView mPeizhengyuanNumb;
    @BindView(R.id.add_jiuzhen)
    TextView mAddJiuName;
    @BindView(R.id.yiyuan)
    TextView mYiyuanName;
    @BindView(R.id.keshi)
    TextView mKeshiName;
    @BindView(R.id.jiuzhen_time)
    TextView mSelectTime;
    private ProvinceCityPresenter mPresenter;
    private LoginOutDialog dialog;
    private long mJiuPersonId, hospitalId, mCityCode;
    private String name, sfzhm, sjhm, keshi_name, yiyuan, keshiId, mLeftTime, selectTime, mTimeStates;
    private BaseRecyclerRefreshAdapter<SelectServiceTimeBean.SelectServiceTimeBeanData> mLeftAdapter;
    private List<SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData> mRightList;
    private BaseRecyclerRefreshAdapter<SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData> mRightAdapter;

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getProvinceCityList();
    }

    @Override
    public int getLayoutResource() {
        return R.layout.fragment_home_new_view;
    }

    @Override
    public void initLayoutView() {
        Glide.with(getActivity()).load(R.drawable.logo)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(mIoncImage);
        mLinearLayout.getBackground().setAlpha(50);
        CardUtils.setCardShadowColor(cardView, getResources().getColor(R.color.base_color), getResources().getColor(R.color.white_color));
        mPresenter = new ProvinceCityPresenter(this);
        SetPersenter(mPresenter);
        if (!HttpNetworkConnectUtils.isNetworkConnected(getActivity())) {
            ToastUtil.showToastINFO(getActivity(), "网络未连接,请确保网络通畅");
        } else {
            mPresenter.getProvinceCityList();
            mPresenter.getMapPoiList("36.03", "103.40");
        }
    }

    @OnClick({R.id.rele_jiu, R.id.linar_top, R.id.liji_yuyue, R.id.rele_yiyuan, R.id.rele_keshi, R.id.rele_time})
    public void OnClick(View veiw) {
        switch (veiw.getId()) {
            case R.id.rele_jiu:
                Intent intent = new Intent(getActivity(), JiuZhengPesonalActivity.class);
                intent.putExtra("person", "person");
                startActivityForResult(intent, 100);
                getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
                break;
            case R.id.rele_yiyuan:
                mKeshiName.setText("");
                mSelectTime.setText("");
                Intent yiyuanIntent = new Intent(getActivity(), HospitalListActivity.class);
                startActivityForResult(yiyuanIntent, 300);
                getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
                break;
            case R.id.rele_keshi:
                if (hospitalId == 0) {
                    ToastUtil.showToastINFO(getActivity(), "请选择就诊医院");
                } else {
                    mSelectTime.setText("");
                    Intent keshi = new Intent(getActivity(), YuyueKeShiActivity.class);
                    keshi.putExtra("keshi", "keshi");
                    keshi.putExtra("yiyuan", yiyuan);
                    keshi.putExtra("hospitalId", hospitalId);
                    startActivityForResult(keshi, 500);
                    getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
                }
                break;
            case R.id.rele_time:
                if (hospitalId == 0) {
                    ToastUtil.showToastINFO(getActivity(), "请选择就诊医院");
                } else {
                    mPresenter.getSelectTime(hospitalId);
                }
                break;
            case R.id.liji_yuyue:
                if (mAddJiuName.getText().toString().trim().equals("")) {
                    ToastUtil.showToastINFO(getActivity(), "请选择就诊人信息");
                    return;
                } else if (mYiyuanName.getText().toString().trim().equals("")) {
                    ToastUtil.showToastINFO(getActivity(), "请选择就诊医院");
                    return;
                } else if (mKeshiName.getText().toString().trim().equals("")) {
                    ToastUtil.showToastINFO(getActivity(), "请选择就诊科室");
                    return;
                } else if (mSelectTime.getText().toString().trim().equals("")) {
                    ToastUtil.showToastINFO(getActivity(), "请选择就诊服务时间");
                    return;
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("name", name);
                    bundle.putString("sfzhm", sfzhm);
                    bundle.putString("yiyuan", yiyuan);
                    bundle.putString("keshi", keshi_name);
                    bundle.putString("shijian", mSelectTime.getText().toString().trim());
                    bundle.putString("mobile", sjhm);
                    bundle.putString("keshiId", keshiId);
                    bundle.putLong("hospitalId", hospitalId);
                    bundle.putString("SelectType", mTimeStates);
                    bundle.putLong("mJiuPersonId", mJiuPersonId);
                    bundle.putString("selectTime", selectTime);
                    openActivityWitchAnimation(YuyueOrderActoivity.class, bundle);
                }
                break;
//            case R.id.linar_top:
////                openActivityWitchAnimation(MapPoiActvity.class);
//                break;
//            case R.id.add_jiuzhen_yiuan:
//                openActivityWitchAnimation(JiuzhenHostptialActivity.class);
//                break;
//            case R.id.linear_add:
//                openActivityWitchAnimation(JiuzhenMsgActivity.class);
//                break;
        }
    }

    @Override
    public void getSelectTime(SelectServiceTimeBean serviceTimeBean) {
        if (serviceTimeBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            SelectTimeDialog dialog = new SelectTimeDialog(getActivity());
            dialog.show();
            RecyclerView mLeftRecyclerView = (RecyclerView) dialog.findViewById(R.id.recycler_left_time);
            RecyclerView mRightRecyclerView = (RecyclerView) dialog.findViewById(R.id.recycler_right_time);
            RecyclerViewLayoutManager.setLinearVerticalManager(getActivity(), mLeftRecyclerView);
            RecyclerViewLayoutManager.setLinearVerticalManager(getActivity(), mRightRecyclerView);
            mLeftTime = serviceTimeBean.getData().get(0).getDay();
            selectTime = serviceTimeBean.getData().get(0).getDay();
            mLeftAdapter = new BaseRecyclerRefreshAdapter<SelectServiceTimeBean.SelectServiceTimeBeanData>(getActivity(), serviceTimeBean.getData(), R.layout.yuyue_keshi_item) {
                @Override
                public void convert(BaseRecyclerHolder holder, SelectServiceTimeBean.SelectServiceTimeBeanData item, int position, boolean isScrolling) throws IOException {
                    holder.setText(R.id.yuyue_item, item.getDay());
                    TextView mItem = holder.getView(R.id.yuyue_item);
                    if (position == getmPosition()) {
                        mItem.setBackgroundColor(Color.parseColor("#F5F5F5"));
                    } else {
                        mItem.setBackgroundColor(Color.parseColor("#ffffff"));
                    }
                }
            };
            mLeftRecyclerView.setAdapter(mLeftAdapter);
            mRightList = serviceTimeBean.getData().get(0).getPeriods();
            mRightAdapter = new BaseRecyclerRefreshAdapter<SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData>(getActivity(), mRightList, R.layout.yuyue_keshi_item) {
                @Override
                public void convert(BaseRecyclerHolder holder, SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData item, int position, boolean isScrolling) throws IOException {
                    holder.setText(R.id.yuyue_item, item.getPeriodDesc());
                    TextView mItem = holder.getView(R.id.yuyue_item);
                    if (position == getmPosition()) {
                        mItem.setBackgroundColor(Color.parseColor("#F5F5F5"));
                    } else {
                        mItem.setBackgroundColor(Color.parseColor("#ffffff"));
                    }
                }
            };
            mRightRecyclerView.setAdapter(mRightAdapter);
            mLeftAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View view, int position) {
                    mRightList = serviceTimeBean.getData().get(position).getPeriods();
                    selectTime = serviceTimeBean.getData().get(position).getDay();
                    mLeftTime = serviceTimeBean.getData().get(position).getDay();
                    mRightAdapter = new BaseRecyclerRefreshAdapter<SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData>(getActivity(), mRightList, R.layout.yuyue_keshi_item) {
                        @Override
                        public void convert(BaseRecyclerHolder holder, SelectServiceTimeBean.SelectServiceTimeBeanData.PeriodsData item, int position, boolean isScrolling) throws IOException {
                            holder.setText(R.id.yuyue_item, item.getPeriodDesc());
                            TextView mItem = holder.getView(R.id.yuyue_item);
                            if (position == getmPosition()) {
                                mItem.setBackgroundColor(Color.parseColor("#F5F5F5"));
                            } else {
                                mItem.setBackgroundColor(Color.parseColor("#ffffff"));
                            }
                        }
                    };
                    mRightRecyclerView.setAdapter(mRightAdapter);
                    mRightAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(RecyclerView parent, View view, int position) {
                            mTimeStates = mRightList.get(position).getPeriod();
                            mSelectTime.setVisibility(View.VISIBLE);
                            mSelectTime.setText(mLeftTime + "日" + mRightList.get(position).getPeriodDesc());
                            dialog.dismiss();
                        }
                    });
                    mLeftAdapter.changeState(position);
                    mLeftAdapter.notifyDataSetChanged();
                }
            });

            mRightAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View view, int position) {
                    mTimeStates = mRightList.get(position).getPeriod();
                    mSelectTime.setVisibility(View.VISIBLE);
                    mSelectTime.setText(mLeftTime + "日" + mRightList.get(position).getPeriodDesc());
                    dialog.dismiss();
                }
            });
        } else {
            ToastUtil.showToastINFO(getActivity(), serviceTimeBean.getMsg());
        }
    }

    @Override
    public void getMapPoiList(MapPoiListBean maplist) {
        if (maplist.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            for (MapPoiListBean.MapPoiListBeanData mapPoi : maplist.getData()) {
                mPeizhengyuanNumb.setText("有" + mapPoi.getPzvos().size() + "位陪诊员在线,预计6分钟内等待接单");
            }
        } else {
            ToastUtil.showToastINFO(getActivity(), maplist.getMsg());
        }
    }

    /**
     * @param province 获取城市信息列表
     */
    @Override
    public void getProvinceCityList(ProvinceCityEntity province) {
        if (province != null) {
            if (province.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
                for (ProvinceCityEntity.ProvinceCityEntityData provice : province.getData()) {
                    for (ProvinceCityEntity.ProvinceCityEntityData.ProvinceCityEntityDataCities city : provice.getCities()) {
                        mCityCode = city.getCityId();
                        mAddress.setText(city.getCityName());
                        SPUtils.put(getActivity(), "cityId", mCityCode + "");
                    }
                }
            } else if (province.getCode() == 401) {
                if (dialog == null) {
                    dialog = new LoginOutDialog(getActivity());
                }
                if (dialog != null && !dialog.isShowing()) {
                    dialog.show();
                }
                dialog.findViewById(R.id.circler_delete).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialog.findViewById(R.id.to_login).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        openActivityWitchAnimation(UserLoginActivity.class);
                        SPUtils.clear(getActivity());
                        getActivity().finish();
                        dialog.dismiss();
                    }
                });
            } else {
                ToastUtil.showToastINFO(getActivity(), province.getMsg());
            }
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 200) {
            mJiuPersonId = data.getLongExtra("mJiuPersonId", 0);
            name = data.getStringExtra("name");
            sfzhm = data.getStringExtra("sfzhm");
            sjhm = data.getStringExtra("sjhm");
            mAddJiuName.setVisibility(View.VISIBLE);
            mAddJiuName.setText(name + "  " + sjhm);
        } else if (requestCode == 300 && resultCode == 400) {
            hospitalId = data.getLongExtra("hospitalId", 0);
            yiyuan = data.getStringExtra("yiyuan");
            mYiyuanName.setVisibility(View.VISIBLE);
            mYiyuanName.setText(yiyuan);
        } else if (requestCode == 500 && resultCode == 600) {
            mKeshiName.setVisibility(View.VISIBLE);
            keshi_name = data.getStringExtra("keshi");
            mKeshiName.setText(data.getStringExtra("keshi"));
            keshiId = data.getStringExtra("keshiId");
        }
    }

    @Override
    public void SetPersenter(Object persenter) {
    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastINFO(getActivity(), mErrorMsg);

    }
}
